$(function () {
    init();
    function init() {
        eventList();
    }


    function eventList() {
        //点击登录按钮,验证合法性
        $('#login_btn').on('tap', function () {
            var user_txt = $('#user_txt').val().trim();
            var pwd_txt = $('#pwd_txt').val().trim();
            if (!user_txt || !$.checkPhone(user_txt)) {
                mui.toast('手机号码错误');
                return;
            }
            if (!pwd_txt || pwd_txt.length < 6) {
                mui.toast('密码错误');
                return;
            }
            //通过上面的验证之后,登录
            var params = {
                username: user_txt,
                password: pwd_txt
            }
            $.ajaxSettings.beforeSend();
            $.post(location.origin + '/user/login', params, function (ret) {
                $.ajaxSettings.complete();
                console.log(ret);
                if (ret && ret.success) {
                    mui.toast('登录成功');
                    setTimeout(function () {
                        //判断回话储存中是否有pageName
                        if (sessionStorage.getItem('pageName')) {
                            //如果会话储存中有相应的href地址那么久跳转到相应的页面
                            var href = sessionStorage.getItem('pageName');
                            location.href = href;
                        } else {
                            location.href = './index.html';
                        }
                    }, 1000);
                } else {
                    mui.toast(ret.message);
                }
            })
        })
    }
})